package com.cuit.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.mapper.BookKeepMapper;
import com.cuit.mapper.BookMapper;
import com.cuit.pojo.Book;
import com.cuit.pojo.BookKeep;
import com.cuit.pojo.BookUser;
import com.cuit.result.R;
import com.cuit.service.BookKeepService;
import com.cuit.service.BookService;
import com.cuit.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@CrossOrigin //开放前端的跨域访问
@RestController
@RequestMapping("/bookFB")
public class BookFBListController {
    @Resource
    private BookKeepService bookKeepService;


    @Autowired
    private BookMapper bookMapper;

    @Resource
    private FileService fileService;

    @GetMapping("/list/{userId}")
    @ApiOperation("获得所有图书测试接口")
    public R getAllBook(@PathVariable Integer userId){
        QueryWrapper<Book> queryWrapper= new QueryWrapper();
        queryWrapper.like("userid",userId);
        List<Book> books=bookMapper.selectList(queryWrapper);
        books.forEach(bookKeepList -> {
            bookKeepList.setBookimg(fileService.allPath(bookKeepList.getBookimg()));
        });
        return R.ok().data("bookList2",books);
    }

    @PostMapping("/list")
    @ApiOperation("根据名字搜索图书")
    public R getBookByName(@RequestBody String bookName){
        JSONObject jsonObject= JSON.parseObject(bookName);
        HashMap<String, Object> map = JSON.parseObject(String.valueOf(jsonObject), new TypeReference<HashMap<String, Object>>() {});
        String bookName2=map.get("bookName").toString();
        Integer userId=(Integer) map.get("userId");

        QueryWrapper<Book> queryWrapper= new QueryWrapper();
        queryWrapper.like("bookname",bookName2).like("userId",userId);
        List<Book> books= bookMapper.selectList(queryWrapper);
        books.forEach(bookKeepList -> {
            bookKeepList.setBookimg(fileService.allPath(bookKeepList.getBookimg()));
        });
        return R.ok().data("bookKeepList2",books);
//        List<BookKeep> list = new ArrayList<>();
//        if(bookKeep != null) {
//            bookKeep.setBookImg(fileService.allPath(bookKeep.getBookImg()));
//            list.add(bookKeep);
//            return R.ok().data("bookList",list);
//        }else {
//            return R.error();
//        }
    }

    @PostMapping("/delete")
    @ApiOperation("删除图书")
    public R deleteById(@RequestBody String id){
        JSONObject jsonObject=JSON.parseObject(id);
        HashMap<String, Object> map = JSON.parseObject(String.valueOf(jsonObject), new TypeReference<HashMap<String, Object>>() {});
        Integer bookId=(Integer) map.get("bookId");
        Integer userId=(Integer) map.get("userId");
        System.out.println(id);
        System.out.println(bookId);
        System.out.println(userId);
        int delete=bookMapper.deleteById(bookId);
        if(delete!=0){
            return getAllBook(userId);
        }else{
            return R.error();
        }
    }
}
